home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / ld / emultempl / sunos.em < prev    next >
Encoding:
Text File  |  1996-07-04  |  26.5 KB  |  1,003 lines

  1. # This shell script emits a C file. -*- C -*-
  2. # It does some substitutions.
  3. cat >e${EMULATION_NAME}.c <<EOF
  4. /* This file is is generated by a shell script.  DO NOT EDIT! */
  5.  
  6. /* SunOS emulation code for ${EMULATION_NAME}
  7.    Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
  8.    Written by Steve Chamberlain <sac@cygnus.com>
  9.    SunOS shared library support by Ian Lance Taylor <ian@cygnus.com>
  10.  
  11. This file is part of GLD, the Gnu Linker.
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  26.  
  27. #define TARGET_IS_${EMULATION_NAME}
  28.  
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31.  
  32. #include "bfd.h"
  33. #include "sysdep.h"
  34. #include "bfdlink.h"
  35. #include "libiberty.h"
  36.  
  37. #include "ld.h"
  38. #include "ldmain.h"
  39. #include "ldemul.h"
  40. #include "ldfile.h"
  41. #include "ldmisc.h"
  42. #include "ldexp.h"
  43. #include "ldlang.h"
  44.  
  45. #ifdef HAVE_DIRENT_H
  46. # include <dirent.h>
  47. #else
  48. # define dirent direct
  49. # ifdef HAVE_SYS_NDIR_H
  50. #  include <sys/ndir.h>
  51. # endif
  52. # ifdef HAVE_SYS_DIR_H
  53. #  include <sys/dir.h>
  54. # endif
  55. # ifdef HAVE_NDIR_H
  56. #  include <ndir.h>
  57. # endif
  58. #endif
  59.  
  60. static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
  61. static void gld${EMULATION_NAME}_set_symbols PARAMS ((void));
  62. static void gld${EMULATION_NAME}_create_output_section_statements
  63.   PARAMS ((void));
  64. static void gld${EMULATION_NAME}_find_so
  65.   PARAMS ((lang_input_statement_type *));
  66. static char *gld${EMULATION_NAME}_search_dir
  67.   PARAMS ((const char *, const char *, boolean *));
  68. static void gld${EMULATION_NAME}_after_open PARAMS ((void));
  69. static void gld${EMULATION_NAME}_check_needed
  70.   PARAMS ((lang_input_statement_type *));
  71. static boolean gld${EMULATION_NAME}_search_needed
  72.   PARAMS ((const char *, const char *));
  73. static boolean gld${EMULATION_NAME}_try_needed
  74.   PARAMS ((const char *, const char *));
  75. static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
  76. static void gld${EMULATION_NAME}_find_assignment
  77.   PARAMS ((lang_statement_union_type *));
  78. static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
  79. static void gld${EMULATION_NAME}_count_need
  80.   PARAMS ((lang_input_statement_type *));
  81. static void gld${EMULATION_NAME}_set_need
  82.   PARAMS ((lang_input_statement_type *));
  83. static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
  84.  
  85. static void
  86. gld${EMULATION_NAME}_before_parse()
  87. {
  88.   ldfile_output_architecture = bfd_arch_${ARCH};
  89.   config.dynamic_link = true;
  90. }
  91.  
  92. /* This is called after the command line arguments have been parsed,
  93.    but before the linker script has been read.  If this is a native
  94.    linker, we add the directories in LD_LIBRARY_PATH to the search
  95.    list.  */
  96.  
  97. static void
  98. gld${EMULATION_NAME}_set_symbols ()
  99. {
  100. EOF
  101. if [ "x${host}" = "x${target}" ] ; then
  102.   if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
  103. cat >>e${EMULATION_NAME}.c <<EOF
  104.   const char *env;
  105.  
  106.   env = (const char *) getenv ("LD_LIBRARY_PATH");
  107.   if (env != NULL)
  108.     {
  109.       char *l;
  110.  
  111.       l = xstrdup (env);
  112.       while (1)
  113.     {
  114.       char *c;
  115.  
  116.       c = strchr (l, ':');
  117.       if (c != NULL)
  118.         *c++ = '\0';
  119.       if (*l != '\0')
  120.         ldfile_add_library_path (l, false);
  121.       if (c == NULL)
  122.         break;
  123.       l = c;
  124.     }
  125.     }
  126. EOF
  127.   fi
  128. fi
  129. cat >>e${EMULATION_NAME}.c <<EOF
  130. }
  131.  
  132. /* Despite the name, we use this routine to search for dynamic
  133.    libraries.  On SunOS this requires a directory search.  We need to
  134.    find the .so file with the highest version number.  The user may
  135.    restrict the major version by saying, e.g., -lc.1.  Also, if we
  136.    find a .so file, we need to look for a the same file after
  137.    replacing .so with .sa; if it exists, it will be an archive which
  138.    provide some initializations for data symbols, and we need to
  139.    search it after including the .so file.  */
  140.  
  141. static void
  142. gld${EMULATION_NAME}_create_output_section_statements ()
  143. {
  144.   lang_for_each_input_file (gld${EMULATION_NAME}_find_so);
  145. }
  146.  
  147. /* Search the directory for a .so file for each library search.  */
  148.  
  149. static void
  150. gld${EMULATION_NAME}_find_so (inp)
  151.      lang_input_statement_type *inp;
  152. {
  153.   search_dirs_type *search;
  154.   char *found;
  155.   char *alc;
  156.   struct stat st;
  157.  
  158.   if (! inp->search_dirs_flag
  159.       || ! inp->is_archive
  160.       || ! inp->dynamic)
  161.     return;
  162.  
  163.   ASSERT (strncmp (inp->local_sym_name, "-l", 2) == 0);
  164.  
  165.   for (search = search_head; search != NULL; search = search->next)
  166.     {
  167.       boolean found_static;
  168.  
  169.       found = gld${EMULATION_NAME}_search_dir (search->name, inp->filename,
  170.                            &found_static);
  171.       if (found != NULL || found_static)
  172.     break;
  173.     }
  174.  
  175.   if (found == NULL)
  176.     {
  177.       /* We did not find a matching .so file.  This isn't an error,
  178.      since there might still be a matching .a file, which will be
  179.      found by the usual search.  */
  180.       return;
  181.     }
  182.  
  183.   /* Replace the filename with the one we have found.  */
  184.   alc = (char *) xmalloc (strlen (search->name) + strlen (found) + 2);
  185.   sprintf (alc, "%s/%s", search->name, found);
  186.   inp->filename = alc;
  187.  
  188.   /* Turn off the search_dirs_flag to prevent ldfile_open_file from
  189.      searching for this file again.  */
  190.   inp->search_dirs_flag = false;
  191.  
  192.   free (found);
  193.  
  194.   /* Now look for the same file name, but with .sa instead of .so.  If
  195.      found, add it to the list of input files.  */
  196.   alc = (char *) xmalloc (strlen (inp->filename) + 1);
  197.   strcpy (alc, inp->filename);
  198.   strstr (alc, ".so.")[2] = 'a';
  199.   if (stat (alc, &st) != 0)
  200.     free (alc);
  201.   else
  202.     {
  203.       lang_input_statement_type *sa;
  204.  
  205.       /* Add the .sa file to the statement list just before the .so
  206.      file.  This is really a hack.  */
  207.       sa = ((lang_input_statement_type *)
  208.         xmalloc (sizeof (lang_input_statement_type)));
  209.       *sa = *inp;
  210.  
  211.       inp->filename = alc;
  212.       inp->local_sym_name = alc;
  213.  
  214.       inp->header.next = (lang_statement_union_type *) sa;
  215.       inp->next_real_file = (lang_statement_union_type *) sa;
  216.     }
  217. }
  218.  
  219. /* Search a directory for a .so file.  */
  220.  
  221. static char *
  222. gld${EMULATION_NAME}_search_dir (dirname, filename, found_static)
  223.      const char *dirname;
  224.      const char *filename;
  225.      boolean *found_static;
  226. {
  227.   int force_maj, force_min;
  228.   const char *dot;
  229.   unsigned int len;
  230.   char *alc;
  231.   char *found;
  232.   int max_maj, max_min;
  233.   DIR *dir;
  234.   struct dirent *entry;
  235.  
  236.   *found_static = false;
  237.  
  238.   force_maj = -1;
  239.   force_min = -1;
  240.   dot = strchr (filename, '.');
  241.   if (dot == NULL)
  242.     {
  243.       len = strlen (filename);
  244.       alc = NULL;
  245.     }
  246.   else
  247.     {
  248.       force_maj = atoi (dot + 1);
  249.  
  250.       len = dot - filename;
  251.       alc = (char *) xmalloc (len + 1);
  252.       strncpy (alc, filename, len);
  253.       alc[len] = '\0';
  254.       filename = alc;
  255.  
  256.       dot = strchr (dot + 1, '.');
  257.       if (dot != NULL)
  258.     force_min = atoi (dot + 1);
  259.     }
  260.  
  261.   found = NULL;
  262.   max_maj = max_min = 0;
  263.  
  264.   dir = opendir (dirname);
  265.   if (dir == NULL)
  266.     return NULL;
  267.   
  268.   while ((entry = readdir (dir)) != NULL)
  269.     {
  270.       int found_maj, found_min;
  271.  
  272.       if (strncmp (entry->d_name, "lib", 3) != 0
  273.       || strncmp (entry->d_name + 3, filename, len) != 0)
  274.     continue;
  275.  
  276.       if (dot == NULL
  277.       && strcmp (entry->d_name + 3 + len, ".a") == 0)
  278.     {
  279.       *found_static = true;
  280.       continue;
  281.     }
  282.  
  283.       if (strncmp (entry->d_name + 3 + len, ".so", 3) != 0)
  284.     continue;
  285.  
  286.       /* We've found a .so file.  Work out the major and minor
  287.      version numbers.  */
  288.       found_maj = 0;
  289.       found_min = 0;
  290.       sscanf (entry->d_name + 3 + len, ".so.%d.%d",
  291.           &found_maj, &found_min);
  292.  
  293.       if ((force_maj != -1 && force_maj != found_maj)
  294.       || (force_min != -1 && force_min != found_min))
  295.     continue;
  296.  
  297.       /* We've found a match for the name we are searching for.  See
  298.      if this is the version we should use.  If the major and minor
  299.      versions match, we use the last entry in alphabetical order;
  300.      I don't know if this is how SunOS distinguishes libc.so.1.8
  301.      from libc.so.1.8.1, but it ought to suffice.  */
  302.       if (found == NULL
  303.       || (found_maj > max_maj)
  304.       || (found_maj == max_maj
  305.           && (found_min > max_min
  306.           || (found_min == max_min
  307.               && strcmp (entry->d_name, found) > 0))))
  308.     {
  309.       if (found != NULL)
  310.         free (found);
  311.       found = (char *) xmalloc (strlen (entry->d_name) + 1);
  312.       strcpy (found, entry->d_name);
  313.       max_maj = found_maj;
  314.       max_min = found_min;
  315.     }
  316.     }
  317.  
  318.   closedir (dir);
  319.  
  320.   if (alc != NULL)
  321.     free (alc);
  322.  
  323.   return found;
  324. }
  325.  
  326. /* These variables are required to pass information back and forth
  327.    between after_open and check_needed.  */
  328.  
  329. static struct bfd_link_needed_list *global_needed;
  330. static boolean global_found;
  331.  
  332. /* This is called after all the input files have been opened.  */
  333.  
  334. static void
  335. gld${EMULATION_NAME}_after_open ()
  336. {
  337.   struct bfd_link_needed_list *needed, *l;
  338.  
  339.   /* We only need to worry about this when doing a final link.  */
  340.   if (link_info.relocateable || link_info.shared)
  341.     return;
  342.  
  343.   /* Get the list of files which appear in ld_need entries in dynamic
  344.      objects included in the link.  For each such file, we want to
  345.      track down the corresponding library, and include the symbol
  346.      table in the link.  This is what the runtime dynamic linker will
  347.      do.  Tracking the files down here permits one dynamic object to
  348.      include another without requiring special action by the person
  349.      doing the link.  Note that the needed list can actually grow
  350.      while we are stepping through this loop.  */
  351.   needed = bfd_sunos_get_needed_list (output_bfd, &link_info);
  352.   for (l = needed; l != NULL; l = l->next)
  353.     {
  354.       struct bfd_link_needed_list *ll;
  355.       const char *lname;
  356.       const char *lib_path;
  357.       search_dirs_type *search;
  358.  
  359.       lname = l->name;
  360.  
  361.       /* If we've already seen this file, skip it.  */
  362.       for (ll = needed; ll != l; ll = ll->next)
  363.     if (strcmp (ll->name, lname) == 0)
  364.       break;
  365.       if (ll != l)
  366.     continue;
  367.  
  368.       /* See if this file was included in the link explicitly.  */
  369.       global_needed = l;
  370.       global_found = false;
  371.       lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
  372.       if (global_found)
  373.     continue;
  374.  
  375.       if (strncmp (lname, "-l", 2) != 0)
  376.     {
  377.       bfd *abfd;
  378.  
  379.       abfd = bfd_openr (lname, bfd_get_target (output_bfd));
  380.       if (abfd != NULL)
  381.         {
  382.           if (! bfd_check_format (abfd, bfd_object))
  383.         {
  384.           (void) bfd_close (abfd);
  385.           abfd = NULL;
  386.         }
  387.         }
  388.       if (abfd != NULL)
  389.         {
  390.           if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
  391.         {
  392.           (void) bfd_close (abfd);
  393.           abfd = NULL;
  394.         }
  395.         }
  396.       if (abfd != NULL)
  397.         {
  398.           /* We've found the needed dynamic object.  */
  399.           if (! bfd_link_add_symbols (abfd, &link_info))
  400.         einfo ("%F%B: could not read symbols: %E\n", abfd);
  401.         }
  402.       else
  403.         {
  404.           einfo ("%P: warning: %s, needed by %B, not found\n",
  405.              lname, l->by);
  406.         }
  407.  
  408.       continue;
  409.     }
  410.  
  411.       lname += 2;
  412.  
  413.       /* We want to search for the file in the same way that the
  414.      dynamic linker will search.  That means that we want to use
  415.      rpath_link, rpath or -L, then the environment variable
  416.      LD_LIBRARY_PATH (native only), then (if rpath was used) the
  417.      linker script LIB_SEARCH_DIRS.  */
  418.       if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
  419.                           lname))
  420.     continue;
  421.       if (command_line.rpath != NULL)
  422.     {
  423.       if (gld${EMULATION_NAME}_search_needed (command_line.rpath, lname))
  424.         continue;
  425.     }
  426.       else
  427.     {
  428.       for (search = search_head; search != NULL; search = search->next)
  429.         if (gld${EMULATION_NAME}_try_needed (search->name, lname))
  430.           break;
  431.       if (search != NULL)
  432.         continue;
  433.     }
  434. EOF
  435. if [ "x${host}" = "x${target}" ] ; then
  436.   if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
  437. cat >>e${EMULATION_NAME}.c <<EOF
  438.       lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
  439.       if (gld${EMULATION_NAME}_search_needed (lib_path, lname))
  440.     continue;
  441. EOF
  442.   fi
  443. fi
  444. cat >>e${EMULATION_NAME}.c <<EOF
  445.       if (command_line.rpath != NULL)
  446.     {
  447.       for (search = search_head; search != NULL; search = search->next)
  448.         {
  449.           if (search->cmdline)
  450.         continue;
  451.           if (gld${EMULATION_NAME}_try_needed (search->name, lname))
  452.         break;
  453.         }
  454.       if (search != NULL)
  455.         continue;
  456.     }
  457.  
  458.       einfo ("%P: warning: %s, needed by %B, not found\n",
  459.          l->name, l->by);
  460.     }
  461. }
  462.  
  463. /* Search for a needed file in a path.  */
  464.  
  465. static boolean
  466. gld${EMULATION_NAME}_search_needed (path, name)
  467.      const char *path;
  468.      const char *name;
  469. {
  470.   const char *s;
  471.  
  472.   if (path == NULL || *path == '\0')
  473.     return false;
  474.   while (1)
  475.     {
  476.       const char *dir;
  477.       char *dircopy;
  478.  
  479.       s = strchr (path, ':');
  480.       if (s == NULL)
  481.     {
  482.       dircopy = NULL;
  483.       dir = path;
  484.     }
  485.       else
  486.     {
  487.       dircopy = (char *) xmalloc (s - path + 1);
  488.       memcpy (dircopy, path, s - path);
  489.       dircopy[s - path] = '\0';
  490.       dir = dircopy;
  491.     }
  492.  
  493.       if (gld${EMULATION_NAME}_try_needed (dir, name))
  494.     return true;
  495.  
  496.       if (dircopy != NULL)
  497.     free (dircopy);
  498.  
  499.       if (s == NULL)
  500.     break;
  501.       path = s + 1;
  502.     }
  503.  
  504.   return false;      
  505. }
  506.  
  507. /* This function is called for each possible directory for a needed
  508.    dynamic object.  */
  509.  
  510. static boolean
  511. gld${EMULATION_NAME}_try_needed (dir, name)
  512.      const char *dir;
  513.      const char *name;
  514. {
  515.   char *file;
  516.   char *alc;
  517.   boolean ignore;
  518.   bfd *abfd;
  519.  
  520.   file = gld${EMULATION_NAME}_search_dir (dir, name, &ignore);
  521.   if (file == NULL)
  522.     return false;
  523.  
  524.   alc = (char *) xmalloc (strlen (dir) + strlen (file) + 2);
  525.   sprintf (alc, "%s/%s", dir, file);
  526.   free (file);
  527.   abfd = bfd_openr (alc, bfd_get_target (output_bfd));
  528.   if (abfd == NULL)
  529.     return false;
  530.   if (! bfd_check_format (abfd, bfd_object))
  531.     {
  532.       (void) bfd_close (abfd);
  533.       return false;
  534.     }
  535.   if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
  536.     {
  537.       (void) bfd_close (abfd);
  538.       return false;
  539.     }
  540.  
  541.   /* We've found the needed dynamic object.  */
  542.  
  543.   /* Add this file into the symbol table.  */
  544.   if (! bfd_link_add_symbols (abfd, &link_info))
  545.     einfo ("%F%B: could not read symbols: %E\n", abfd);
  546.  
  547.   return true;
  548. }
  549.  
  550. /* See if we have already included a needed object in the link.  This
  551.    does not have to be precise, as it does no harm to include a
  552.    dynamic object more than once.  */
  553.  
  554. static void
  555. gld${EMULATION_NAME}_check_needed (s)
  556.      lang_input_statement_type *s;
  557. {
  558.   if (s->filename == NULL)
  559.     return;
  560.   if (strncmp (global_needed->name, "-l", 2) != 0)
  561.     {
  562.       if (strcmp (s->filename, global_needed->name) == 0)
  563.     global_found = true;
  564.     }
  565.   else
  566.     {
  567.       const char *sname, *lname;
  568.       const char *sdot, *ldot;
  569.       int lmaj, lmin, smaj, smin;
  570.  
  571.       lname = global_needed->name + 2;
  572.  
  573.       sname = strrchr (s->filename, '/');
  574.       if (sname == NULL)
  575.     sname = s->filename;
  576.       else
  577.     ++sname;
  578.  
  579.       if (strncmp (sname, "lib", 3) != 0)
  580.     return;
  581.       sname += 3;
  582.  
  583.       ldot = strchr (lname, '.');
  584.       if (ldot == NULL)
  585.     ldot = lname + strlen (lname);
  586.  
  587.       sdot = strstr (sname, ".so.");
  588.       if (sdot == NULL)
  589.     return;
  590.  
  591.       if (sdot - sname != ldot - lname
  592.       || strncmp (lname, sname, sdot - sname) != 0)
  593.     return;
  594.  
  595.       lmaj = lmin = -1;
  596.       sscanf (ldot, ".%d.%d", &lmaj, &lmin);
  597.       smaj = smin = -1;
  598.       sscanf (sdot, ".so.%d.%d", &smaj, &smin);
  599.       if ((smaj != lmaj && smaj != -1 && lmaj != -1)
  600.       || (smin != lmin && smin != -1 && lmin != -1))
  601.     return;
  602.  
  603.       global_found = true;
  604.     }
  605. }
  606.  
  607. /* We need to use static variables to pass information around the call
  608.    to lang_for_each_statement.  Ick.  */
  609.  
  610. static const char *find_assign;
  611. static boolean found_assign;
  612.  
  613. /* We need to use static variables to pass information around the call
  614.    to lang_for_each_input_file.  Ick.  */
  615.  
  616. static bfd_size_type need_size;
  617. static bfd_size_type need_entries;
  618. static bfd_byte *need_contents;
  619. static bfd_byte *need_pinfo;
  620. static bfd_byte *need_pnames;
  621.  
  622. /* The size of one entry in the .need section, not including the file
  623.    name.  */
  624.  
  625. #define NEED_ENTRY_SIZE (16)
  626.  
  627. /* This is called after the sections have been attached to output
  628.    sections, but before any sizes or addresses have been set.  */
  629.  
  630. static void
  631. gld${EMULATION_NAME}_before_allocation ()
  632. {
  633.   struct bfd_link_hash_entry *hdyn = NULL;
  634.   asection *sneed;
  635.   asection *srules;
  636.   asection *sdyn;
  637.  
  638.   /* The SunOS native linker creates a shared library whenever there
  639.      are any undefined symbols in a link, unless -e is used.  This is
  640.      pretty weird, but we are compatible.  */
  641.   if (! link_info.shared && ! link_info.relocateable && ! entry_from_cmdline)
  642.     {
  643.       struct bfd_link_hash_entry *h;
  644.       
  645.       for (h = link_info.hash->undefs; h != NULL; h = h->next)
  646.     {
  647.       if (h->type == bfd_link_hash_undefined
  648.           && h->u.undef.abfd != NULL
  649.           && (h->u.undef.abfd->flags & DYNAMIC) == 0
  650.           && strcmp (h->root.string, "__DYNAMIC") != 0
  651.           && strcmp (h->root.string, "__GLOBAL_OFFSET_TABLE_") != 0)
  652.         {
  653.           find_assign = h->root.string;
  654.           found_assign = false;
  655.           lang_for_each_statement (gld${EMULATION_NAME}_find_assignment);
  656.           if (! found_assign)
  657.         {
  658.           link_info.shared = true;
  659.           break;
  660.         }
  661.         }
  662.     }
  663.     }
  664.  
  665.   if (link_info.shared)
  666.     {
  667.       lang_output_section_statement_type *os;
  668.  
  669.       /* Set the .text section to start at 0x20, not 0x2020.  FIXME:
  670.          This is too magical.  */
  671.       os = lang_output_section_statement_lookup (".text");
  672.       if (os->addr_tree == NULL)
  673.     os->addr_tree = exp_intop (0x20);
  674.     }
  675.  
  676.   /* We need to create a __DYNAMIC symbol.  We don't do this in the
  677.      linker script because we want to set the value to the start of
  678.      the dynamic section if there is one, or to zero if there isn't
  679.      one.  We need to create the symbol before calling
  680.      size_dynamic_sections, although we can't set the value until
  681.      afterward.  */
  682.   if (! link_info.relocateable)
  683.     {
  684.       hdyn = bfd_link_hash_lookup (link_info.hash, "__DYNAMIC", true, false,
  685.                    false);
  686.       if (hdyn == NULL)
  687.     einfo ("%P%F: bfd_link_hash_lookup: %E\n");
  688.       if (! bfd_sunos_record_link_assignment (output_bfd, &link_info,
  689.                           "__DYNAMIC"))
  690.     einfo ("%P%F: failed to record assignment to __DYNAMIC: %E\n");
  691.     }
  692.  
  693.   /* If we are going to make any variable assignments, we need to let
  694.      the backend linker know about them in case the variables are
  695.      referred to by dynamic objects.  */
  696.   lang_for_each_statement (gld${EMULATION_NAME}_find_assignment);
  697.  
  698.   /* Let the backend linker work out the sizes of any sections
  699.      required by dynamic linking.  */
  700.   if (! bfd_sunos_size_dynamic_sections (output_bfd, &link_info, &sdyn,
  701.                      &sneed, &srules))
  702.     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
  703.  
  704.   if (sneed != NULL)
  705.     {
  706.       /* Set up the .need section.  See the description of the ld_need
  707.      field in include/aout/sun4.h.  */
  708.  
  709.       need_entries = 0;
  710.       need_size = 0;
  711.  
  712.       lang_for_each_input_file (gld${EMULATION_NAME}_count_need);
  713.  
  714.       /* We should only have a .need section if we have at least one
  715.      dynamic object.  */
  716.       ASSERT (need_entries != 0);
  717.  
  718.       sneed->_raw_size = need_size;
  719.       sneed->contents = (bfd_byte *) xmalloc (need_size);
  720.  
  721.       need_contents = sneed->contents;
  722.       need_pinfo = sneed->contents;
  723.       need_pnames = sneed->contents + need_entries * 16;
  724.  
  725.       lang_for_each_input_file (gld${EMULATION_NAME}_set_need);
  726.  
  727.       ASSERT ((bfd_size_type) (need_pnames - sneed->contents) == need_size);
  728.     }
  729.  
  730.   if (srules != NULL)
  731.     {
  732.       /* Set up the .rules section.  This is just a PATH like string
  733.      of the -L arguments given on the command line.  We permit the
  734.      user to specify the directories using the -rpath command line
  735.      option.  */
  736.       if (command_line.rpath)
  737.     {
  738.       srules->_raw_size = strlen (command_line.rpath);
  739.       srules->contents = (bfd_byte *) command_line.rpath;
  740.     }
  741.       else
  742.     {
  743.       unsigned int size;
  744.       search_dirs_type *search;
  745.  
  746.       size = 0;
  747.       for (search = search_head; search != NULL; search = search->next)
  748.         if (search->cmdline)
  749.           size += strlen (search->name) + 1;
  750.       srules->_raw_size = size;
  751.       if (size > 0)
  752.         {
  753.           char *p;
  754.  
  755.           srules->contents = (bfd_byte *) xmalloc (size);
  756.           p = (char *) srules->contents;
  757.           *p = '\0';
  758.           for (search = search_head; search != NULL; search = search->next)
  759.         {
  760.           if (search->cmdline)
  761.             {
  762.               if (p != (char *) srules->contents)
  763.             *p++ = ':';
  764.               strcpy (p, search->name);
  765.               p += strlen (p);
  766.             }
  767.         }
  768.         }
  769.     }
  770.     }
  771.  
  772.   /* We must assign a value to __DYNAMIC.  It should be zero if we are
  773.      not doing a dynamic link, or the start of the .dynamic section if
  774.      we are doing one.  */
  775.   if (! link_info.relocateable)
  776.     {
  777.       hdyn->type = bfd_link_hash_defined;
  778.       hdyn->u.def.value = 0;
  779.       if (sdyn != NULL)
  780.     hdyn->u.def.section = sdyn;
  781.       else
  782.     hdyn->u.def.section = bfd_abs_section_ptr;
  783.     }
  784. }
  785.  
  786. /* This is called by the before_allocation routine via
  787.    lang_for_each_statement.  It does one of two things: if the
  788.    variable find_assign is set, it sets found_assign if it finds an
  789.    assignment to that variable; otherwise it tells the backend linker
  790.    about all assignment statements, in case they are assignments to
  791.    symbols which are referred to by dynamic objects.  */
  792.  
  793. static void
  794. gld${EMULATION_NAME}_find_assignment (s)
  795.      lang_statement_union_type *s;
  796. {
  797.   if (s->header.type == lang_assignment_statement_enum
  798.       && (find_assign == NULL || ! found_assign))
  799.     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
  800. }
  801.  
  802. /* Look through an expression for an assignment statement.  */
  803.  
  804. static void
  805. gld${EMULATION_NAME}_find_exp_assignment (exp)
  806.      etree_type *exp;
  807. {
  808.   switch (exp->type.node_class)
  809.     {
  810.     case etree_assign:
  811.       if (find_assign != NULL)
  812.     {
  813.       if (strcmp (find_assign, exp->assign.dst) == 0)
  814.         found_assign = true;
  815.       return;
  816.     }
  817.  
  818.       if (strcmp (exp->assign.dst, ".") != 0)
  819.     {
  820.       if (! bfd_sunos_record_link_assignment (output_bfd, &link_info,
  821.                           exp->assign.dst))
  822.         einfo ("%P%F: failed to record assignment to %s: %E\n",
  823.            exp->assign.dst);
  824.     }
  825.       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
  826.       break;
  827.  
  828.     case etree_binary:
  829.       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
  830.       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
  831.       break;
  832.  
  833.     case etree_trinary:
  834.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
  835.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
  836.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
  837.       break;
  838.  
  839.     case etree_unary:
  840.       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
  841.       break;
  842.  
  843.     default:
  844.       break;
  845.     }
  846. }
  847.  
  848. /* Work out the size of the .need section, and the number of entries.
  849.    The backend will set the ld_need field of the dynamic linking
  850.    information to point to the .need section.  See include/aout/sun4.h
  851.    for more information.  */
  852.  
  853. static void
  854. gld${EMULATION_NAME}_count_need (inp)
  855.      lang_input_statement_type *inp;
  856. {
  857.   if (inp->the_bfd != NULL
  858.       && (inp->the_bfd->flags & DYNAMIC) != 0)
  859.     {
  860.       ++need_entries;
  861.       need_size += NEED_ENTRY_SIZE;
  862.       if (! inp->is_archive)
  863.     need_size += strlen (inp->filename) + 1;
  864.       else
  865.     {
  866.       ASSERT (inp->local_sym_name[0] == '-'
  867.           && inp->local_sym_name[1] == 'l');
  868.       need_size += strlen (inp->local_sym_name + 2) + 1;
  869.     }
  870.     }
  871. }
  872.  
  873. /* Fill in the contents of the .need section.  */
  874.  
  875. static void
  876. gld${EMULATION_NAME}_set_need (inp)
  877.      lang_input_statement_type *inp;
  878. {
  879.   if (inp->the_bfd != NULL
  880.       && (inp->the_bfd->flags & DYNAMIC) != 0)
  881.     {
  882.       bfd_size_type c;
  883.  
  884.       /* To really fill in the .need section contents, we need to know
  885.      the final file position of the section, but we don't.
  886.      Instead, we use offsets, and rely on the BFD backend to
  887.      finish the section up correctly.  FIXME: Talk about lack of
  888.      referential locality.  */
  889.       bfd_put_32 (output_bfd, need_pnames - need_contents, need_pinfo);
  890.       if (! inp->is_archive)
  891.     {
  892.       bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 4);
  893.       bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 8);
  894.       bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 10);
  895.       strcpy (need_pnames, inp->filename);
  896.     }
  897.       else
  898.     {
  899.       char *verstr;
  900.       int maj, min;
  901.  
  902.       bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, need_pinfo + 4);
  903.       maj = 0;
  904.       min = 0;
  905.       verstr = strstr (inp->filename, ".so.");
  906.       if (verstr != NULL)
  907.         sscanf (verstr, ".so.%d.%d", &maj, &min);
  908.       bfd_put_16 (output_bfd, (bfd_vma) maj, need_pinfo + 8);
  909.       bfd_put_16 (output_bfd, (bfd_vma) min, need_pinfo + 10);
  910.       strcpy (need_pnames, inp->local_sym_name + 2);
  911.     }
  912.  
  913.       c = (need_pinfo - need_contents) / NEED_ENTRY_SIZE;
  914.       if (c + 1 >= need_entries)
  915.     bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 12);
  916.       else
  917.     bfd_put_32 (output_bfd, (bfd_vma) (c + 1) * NEED_ENTRY_SIZE,
  918.             need_pinfo + 12);
  919.  
  920.       need_pinfo += NEED_ENTRY_SIZE;
  921.       need_pnames += strlen (need_pnames) + 1;
  922.     }
  923. }
  924.  
  925. static char *
  926. gld${EMULATION_NAME}_get_script(isfile)
  927.      int *isfile;
  928. EOF
  929.  
  930. if test -n "$COMPILE_IN"
  931. then
  932. # Scripts compiled in.
  933.  
  934. # sed commands to quote an ld script as a C string.
  935. sc='s/["\\]/\\&/g
  936. s/$/\\n\\/
  937. 1s/^/"/
  938. $s/$/n"/
  939. '
  940.  
  941. cat >>e${EMULATION_NAME}.c <<EOF
  942. {                 
  943.   *isfile = 0;
  944.  
  945.   if (link_info.relocateable == true && config.build_constructors == true)
  946.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
  947.   else if (link_info.relocateable == true)
  948.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
  949.   else if (!config.text_read_only)
  950.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
  951.   else if (!config.magic_demand_paged)
  952.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
  953.   else
  954.     return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
  955. }
  956. EOF
  957.  
  958. else
  959. # Scripts read from the filesystem.
  960.  
  961. cat >>e${EMULATION_NAME}.c <<EOF
  962. {                 
  963.   *isfile = 1;
  964.  
  965.   if (link_info.relocateable == true && config.build_constructors == true)
  966.     return "ldscripts/${EMULATION_NAME}.xu";
  967.   else if (link_info.relocateable == true)
  968.     return "ldscripts/${EMULATION_NAME}.xr";
  969.   else if (!config.text_read_only)
  970.     return "ldscripts/${EMULATION_NAME}.xbn";
  971.   else if (!config.magic_demand_paged)
  972.     return "ldscripts/${EMULATION_NAME}.xn";
  973.   else
  974.     return "ldscripts/${EMULATION_NAME}.x";
  975. }
  976. EOF
  977.  
  978. fi
  979.  
  980. cat >>e${EMULATION_NAME}.c <<EOF
  981.  
  982. struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
  983. {
  984.   gld${EMULATION_NAME}_before_parse,
  985.   syslib_default,
  986.   hll_default,
  987.   after_parse_default,
  988.   gld${EMULATION_NAME}_after_open,
  989.   after_allocation_default,
  990.   set_output_arch_default,
  991.   ldemul_default_target,
  992.   gld${EMULATION_NAME}_before_allocation,
  993.   gld${EMULATION_NAME}_get_script,
  994.   "${EMULATION_NAME}",
  995.   "${OUTPUT_FORMAT}",
  996.   NULL, /* finish */
  997.   gld${EMULATION_NAME}_create_output_section_statements,
  998.   NULL, /* open_dynamic_library */
  999.   NULL, /* place_orphan */
  1000.   gld${EMULATION_NAME}_set_symbols
  1001. };
  1002. EOF
  1003.